using System.Collections.Generic;
using System.Linq;
using SovaDataService;
using SovaDataService.SoDataServices;
using SovaDataService.SoModels;
using SovaDataService.SovaModels;
namespace SovaDataService.SovaDataServices
{
    public class AccountDataService
    {
        /**
         * Adding an account to the database.
         */
        public Account Create(Account account)
        {
            using (var db = new DbContext())
            {
                db.Accounts.Add(account);
                db.SaveChanges();

                return account;
            }
        }

        /**
         * Removing an account from database.
         */
        public bool Delete(int id)
        {
            using (var db = new DbContext())
            {
                Account account = db.Accounts.SingleOrDefault(a => a.Id == id);

                if (account == null)
                    return false;

                db.Accounts.Remove(account);
                db.SaveChanges();

                return true;
            }
        }

        /**
         * Retrieves all accounts. Not implemented
         */
        public IEnumerable<Account> GetAll()
        {
            throw new System.NotImplementedException();
        }

        /**
         * Retrieves the count of the accounts. Not implmented
         */
        public int GetCount()
        {
            throw new System.NotImplementedException();
        }

        /**
         * Retrieves one account by id.
         */
        public Account GetOne(int id)
        {
            using (var db = new DbContext())
            {
                return db.Accounts.SingleOrDefault(a => a.Id == id);
            }
        }

        /**
         * Retrieves one account by email.
         */
        public Account GetAccountByEmail(string email)
        {
            using (var db = new DbContext())
            {
                return db.Accounts.SingleOrDefault(a => a.Email == email);
            }
        }

        /**
         * Checking if the email already exists in the database.
         */
        public bool EmailAlreadyExists(string email)
        {
            using (var db = new DbContext())
            {
                return db.Accounts.Any(a => a.Email == email);
            }
        }
    }
}